//
//  SetGoWorkOrDownTimeViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/4.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

class SetGoWorkOrDownTimeViewController: BaseViewController {
    @IBOutlet var goWorkTimeLabel: UILabel!
    @IBOutlet var downWorkTimeLabel: UILabel!
    var goWorkTime = ""
    var downWorkTime = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        goWorkTimeLabel.text = goWorkTime
        downWorkTimeLabel.text = downWorkTime
        baseSetNavRightButtonWithTitle("保存")
    }
    
    override func baseNavRightButtonPressed(_ button: UIButton) {
        requestSetWorkTime(goTime: self.goWorkTimeLabel.text!, downTime: self.downWorkTimeLabel.text!)
    }

    
    @IBAction func goWorkBtnPress(_ sender: UIButton) {
       
        BRDatePickerView.showDatePicker(withTitle: "上班时间", dateType: .time, defaultSelValue:NSDate.currentDateString(), minDateStr:NSDate.currentDateString(), maxDateStr: "", isAutoSelect: true) { (timeStr) in
            self.goWorkTimeLabel.text = timeStr! + ":00"
        }
    }
    
    
    @IBAction func downWorkBtnPress(_ sender: UIButton) {
        BRDatePickerView.showDatePicker(withTitle: "下班时间", dateType: .time, defaultSelValue:NSDate.currentDateString(), minDateStr:NSDate.currentDateString(), maxDateStr: "", isAutoSelect: true) { (timeStr) in
            self.downWorkTimeLabel.text = timeStr! + ":00"
        }
    }
    
    //设置上下班时间
    func requestSetWorkTime(goTime:String,downTime:String) -> Void {
        
        let param =
            [
                "uid":UserModel.shareUser.data?.uid ?? String(),
                "sid":UserModel.shareUser.data?.storeId ?? String(),
                "go_work_time":goTime,
                "closing_work_time":downTime,
                ]
        Alamofire.request(setWorkTimeUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        hud?.mode = MBProgressHUDMode.text
                        hud?.labelText = "设置上下班时间成功"
                        hud?.show(animated: true, whileExecuting: {
                            Thread.sleep(forTimeInterval: 0.8)
                        }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                        })
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
